package com.ruoyi.youtian.controller;

import com.ruoyi.Gzmx_device.domain.Gzmx_device;
import com.ruoyi.common.annotation.AutoSetDefaultParams;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gzmx_scdw.domain.Gzmx_scdw;
import com.ruoyi.gzmx_scdw.service.impl.Gzmx_scdwServiceImpl;
import com.ruoyi.youtian.domain.*;
import com.ruoyi.youtian.domain.vo.LinearGraphVo;
import com.ruoyi.youtian.domain.vo.PieChartVo;
import com.ruoyi.youtian.domain.vo.PointVo;
import com.ruoyi.youtian.domain.vo.ScenePointVo;
import com.ruoyi.youtian.service.ITSceneCategoryService;
import com.ruoyi.youtian.service.ITWorkspaceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工作区分类Controller
 *
 * @author ruoyi
 * @date 2022-09-07
 */
@Controller
@RequestMapping("/youtian/workspace-category")
public class TWorkspaceCategoryController extends BaseController
{

    private String prefix = "youtian/workspace-category";

    @Autowired
    private ITWorkspaceCategoryService tWorkspaceCategoryService;

    @Autowired
    private ITSceneCategoryService sceneCategoryService;


    //@RequiresPermissions("youtian:workspaceCategory:view")
    @GetMapping()
    public String workspaceCategory()
    {
        return prefix + "/workspace-category";
    }

    /**
     * 查询工作区分类列表
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TWorkspaceCategory tWorkspaceCategory)
    {
        startPage();
        List<TWorkspaceCategory> list = tWorkspaceCategoryService.selectTWorkspaceCategoryList(tWorkspaceCategory);
        return getDataTable(list);
    }

    /**
     * 导出工作区分类列表
     */
    //@PreAuthorize("@ss.hasPermi('yq:workspaceCategory:export')")
    @Log(title = "工作区分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response, TWorkspaceCategory tWorkspaceCategory)
    {
        List<TWorkspaceCategory> list = tWorkspaceCategoryService.selectTWorkspaceCategoryList(tWorkspaceCategory);
        ExcelUtil<TWorkspaceCategory> util = new ExcelUtil<TWorkspaceCategory>(TWorkspaceCategory.class);
        util.exportExcel(response, list, "工作区分类数据");
    }

    /**
     * 获取工作区分类详细信息
     */
    @ResponseBody
    @GetMapping(value = "/{workspaceCategoryId}")
    public AjaxResult getInfo(@PathVariable("workspaceCategoryId") Long workspaceCategoryId)
    {
        return AjaxResult.success(tWorkspaceCategoryService.selectTWorkspaceCategoryByWorkspaceCategoryId(workspaceCategoryId));
    }

    /**
     * 新增工作区分类
     */
    @Log(title = "工作区分类", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    @AutoSetDefaultParams
    public AjaxResult add(@RequestBody TWorkspaceCategory tWorkspaceCategory)
    {
        TWorkspaceCategory param = new TWorkspaceCategory();
        param.setCategoryName(tWorkspaceCategory.getCategoryName());
        if (UserConstants.NOT_UNIQUE.equals(tWorkspaceCategoryService.checkTWorkspaceCategoryNameUnique(param)))
        {
            return AjaxResult.error("新增工作区'" + tWorkspaceCategory.getCategoryName() + "'失败，工作区已存在");
        }
        tWorkspaceCategory.setCreateBy(getLoginName());
        return toAjax(tWorkspaceCategoryService.insertTWorkspaceCategory(tWorkspaceCategory));
    }

    /**
     * 修改工作区分类
     */
    @Log(title = "工作区分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    @AutoSetDefaultParams
    public AjaxResult edit(@RequestBody TWorkspaceCategory tWorkspaceCategory)
    {
        tWorkspaceCategory.setUpdateBy(getLoginName());
        return toAjax(tWorkspaceCategoryService.updateTWorkspaceCategory(tWorkspaceCategory));
    }

    /**
     * 删除工作区分类
     */
    @Log(title = "工作区分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workspaceCategoryId}")
    @ResponseBody
    public AjaxResult list(@PathVariable Long workspaceCategoryId)
    {
        if (tWorkspaceCategoryService.hasChildByWorkspaceCategoryId(workspaceCategoryId))
        {
            return AjaxResult.error("存在下级工作区,不允许删除");
        }
        //  TODO 删除工作区并删除下面相应的设备及下级工作区目录
        return toAjax(tWorkspaceCategoryService.deleteTWorkspaceCategoryByWorkspaceCategoryId(workspaceCategoryId));
    }

    @Autowired
    private Gzmx_scdwServiceImpl  gzmxScdwService;

    /**
     * 基础管理 左侧树结构
     */
    @ResponseBody
    @RequestMapping("/workspaceCategoryTree")
    public AjaxResult workspaceCategoryTree(Gzmx_scdw gzmxScdw)
    {
        List<TreeSelect> treeList = gzmxScdwService.selectGzmx_scdwBasicTreeSelectList(gzmxScdw);
        return AjaxResult.success(treeList);
    }

    /**
     * 为工作区新增点位
     * @param tWorkspaceCategoryPointVo
     * @return
     */
    @ResponseBody
    @PostMapping("/insertPoint")
    public AjaxResult addPoint(@RequestBody TWorkspaceCategoryPointVo tWorkspaceCategoryPointVo){
        tWorkspaceCategoryPointVo.setCreateBy(getLoginName());
        return AjaxResult.success(tWorkspaceCategoryService.insertPoint(tWorkspaceCategoryPointVo));
    }

    /**
     *  删除工作区下点位
     * @param tWorkspaceCategoryPointVo
     * @return
     */
    @ResponseBody
    @DeleteMapping("/deletePoint")
    public AjaxResult deletePoint(@RequestBody TWorkspaceCategoryPointVo tWorkspaceCategoryPointVo){
        return AjaxResult.success(tWorkspaceCategoryService.deletePoint(tWorkspaceCategoryPointVo));
    }


    /**
     *  查询工作区下所有点位列表
     * @param tWorkspaceCategoryPointVo
     * @return
     */
    @GetMapping("/getPointList")
    @ResponseBody
    public TableDataInfo getPointList(TWorkspaceCategoryPointVo tWorkspaceCategoryPointVo){
        if(tWorkspaceCategoryPointVo != null && tWorkspaceCategoryPointVo.getWorkspaceCategoryId() == null) {
            tWorkspaceCategoryPointVo.setWorkspaceCategoryId(-1L);
        }
        ScenePointVo scenePointVo = new ScenePointVo();
        scenePointVo.setSceneId(tWorkspaceCategoryPointVo.getWorkspaceCategoryId());
        if(scenePointVo != null && scenePointVo.getSceneId() == null){
            scenePointVo.setSceneId(-1L);
        }

        List<Long> childTSceneCategoryIdList = sceneCategoryService.getChildTSceneCategoryIdList(scenePointVo.getSceneId());
        startPage();
        List<ScenePointVo> scenePointVoList = sceneCategoryService.selectScenePointVoListBySceneIdList(childTSceneCategoryIdList, scenePointVo.getPointName(), scenePointVo.getSceneName());
        return getDataTable(scenePointVoList);
    }
//        List<Long> childIdList = tWorkspaceCategoryService.findChildIdList(tWorkspaceCategoryPointVo);
//        startPage();
//        List<PointVo> pointVoList = tWorkspaceCategoryService.selectPointListByWorkspaceIdList(childIdList, tWorkspaceCategoryPointVo.getPointName());
//        return getDataTable(pointVoList);
//    }

    /**
     * 查询该设备下所有点位getDevicePointList
     * @param pointVo
     * @return
     */
    @PostMapping("/getDevicePointList")
    @ResponseBody
    public TableDataInfo getDevicePointList(PointVo pointVo){
        startPage();
        List<PointVo> pointVoList = tWorkspaceCategoryService.selectDevicePointList(pointVo);
        return getDataTable(pointVoList);
    }

    /**
     * 线状图
     * @param linearGraphVo
     * @return
     */
    @PostMapping("/getLineargraph")
    @ResponseBody
    public AjaxResult getLineargraph(@RequestBody LinearGraphVo linearGraphVo){
        return tWorkspaceCategoryService.getLineargraph(linearGraphVo);
    }

    /**
     *  查询工作区下所有点位列表(添加搜索条件)
     * @param scenePointVo
     * @return
     */
    @GetMapping("/reogetPointList")
    @ResponseBody
    public TableDataInfo reogetPointList(ScenePointVo scenePointVo){
        startPage();
        List<ScenePointVo> scenePointVoList = sceneCategoryService.selectScenePointVoListBySceneIdListreo(scenePointVo);
        return getDataTable(scenePointVoList);
    }

    /**
     * 饼图
     * @param PieChartVo
     * @return
     */
    @GetMapping("/getPieChart")
    @ResponseBody
    public AjaxResult getPieChart(PieChartVo PieChartVo){
        return tWorkspaceCategoryService.getPieChart(PieChartVo);
    }

    /**
     * 柱图
     * @param PieChartVo
     * @return
     */
    @GetMapping("/getColumndiagram")
    @ResponseBody
    public AjaxResult getColumndiagram(PieChartVo PieChartVo){
        return tWorkspaceCategoryService.getColumndiagram(PieChartVo);
    }

}
